<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    >
      <template #btns>
        <el-button
          v-if="meta.indexOf('IntegralDetailExcel') > -1"
          :loading="exportLoading"
          type="primary"
          @click="batchexport"
        >{{ translate("export") }}</el-button>
      </template>
    </QueryForm>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('xfyh')"
        min-width="220"
        prop="memberInfo"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('xflx')"
        min-width="120"
        prop="sourceName"
      />
      <el-table-column
        align="center"
        :label="translate('szlx')"
        min-width="100"
        prop="type"
      >
        <template slot-scope="scope">
          {{ scope.row.type == "1" ? translate('sr') : translate('zhichu') }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('integralShouZhi')"
        prop="integral"
        min-width="120"
      />
      <el-table-column
        align="center"
        :label="translate('timeShouZhi')"
        prop="createTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="translate('orderNum')"
        min-width="240"
        prop="orderId"
      />
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { integralDetail, venueList, integralDetailExport, consumptionType } from '@/api'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'IntegralDetail',
  components: {
    Pagination
  },
  data() {
    return {
      list: [],
      meta: [],
      exportLoading: false,
      venueList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      queryFormConfig,
      total: 0,
      filters: {},
      listLoading: true
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getConsumptionType()
    this.getVenueList()
  },
  methods: {
    getConsumptionType() {
      consumptionType({ param: {}})
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.queryFormConfig.formItems[3].options = res.recordList
        })
        .catch((err) => {
          console.log(err)
        })
    },
    clearList() {
      this.filters = {
        venueId: this.filters.venueId
      }
      this.getList(1)
    },
    getVenueList() {
      venueList({
        param: { flag: '0' }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode !== '000000') return
          this.venueList = res.recordList
          this.$set(this.filters, 'venueId', res.recordList[0].id)
          const arr = res.recordList
          this.queryFormConfig.formItems[0].options = arr
          this.getList()
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      sessionStorage.setItem('venueId', this.filters.venueId)
      this.listLoading = true
      const { filters, pagination } = this
      // const venueId = sessionStorage.getItem('venueId')
      filters.flag = '1'
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      pagination.firstQueryTime = parseTime(new Date())
      integralDetail({ pagination, param: { ...filters }}).then(res => {
        this.listLoading = false
        if (res.errorCode !== '000000') return
        this.total = res.totalCount
        this.list = res.recordList
      }).catch(() => {
        this.listLoading = false
      })
    },
    batchexport() {
      sessionStorage.setItem('venueId', this.filters.venueId)

      const { filters } = this
      const venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      integralDetailExport({ param: { venueId, ...filters }}).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>
